package com.lxw.question;

public class Q0602 {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //获取两个链表的长度。
        //在短的那个后面补0
        int l1Len = getListLen(l1);
        int l2Len = getListLen(l2);

        if(l1Len < l2Len){
            //补零
            l1 = fillZeroInListEnd(l1,l2Len - l1Len);
        } else if(l1Len > l2Len) {
            l2 = fillZeroInListEnd(l2,l1Len - l2Len);
        }

        //相加
        int add = 0;
        ListNode headNode = null;
        boolean isFirst = true;
        ListNode cur = null;
        boolean isEndAndAdd = false;
        while(l1 != null){
            //l1,l2
            int result = 0;
            if(add != 0){
                result = l1.val + l2.val + add;
                add = 0;
            } else{
                result = l1.val + l2.val;
            }

            int newValue = result % 10;
            add = result /10;
            ListNode newListNode = new ListNode(newValue);
            //cur = newListNode;
            if(isFirst){
                isFirst = false;
                headNode = newListNode;
                cur = newListNode;
            } else{
                //插入新的值往新的链表中
                cur.next = newListNode;
                cur = newListNode;
                newListNode.next = null;
            }
            l1 = l1.next;
            l2 = l2.next;
            if(l1 == null && add != 0){
                isEndAndAdd = true;
            }
        }
        //处理进位的问题
        if(isEndAndAdd){
            ListNode addNode = new ListNode(add);
            cur.next = addNode;
            cur = addNode;
            cur.next = null;
        }
        return headNode;
    }

    /**
     * 获取链表的长度
     * @param l
     * @return
     */
    public static int getListLen(ListNode l){
        int listLen = 0;
        while(l != null){
            listLen++;
            l = l.next;
        }
        return listLen;
    }

    public static void reverseList(ListNode l){
        while(l != null){
            System.out.print(l.val+" ");
            l = l.next;
        }
    }
    /**
     * 在链表的尾部添加元素->0
     * @param minList
     * @param fillNum
     * @return
     */
    public static ListNode fillZeroInListEnd(ListNode minList,int fillNum){
        //在链表的头部添加元素->0
        //找到链表尾部
        ListNode tailNode = minList;
        while(tailNode.next != null){
            tailNode = tailNode.next;
        }
        for(int i = 0 ; i < fillNum ; i++){
            ListNode zeroNode = new ListNode(0);
            tailNode.next = zeroNode;
            tailNode = zeroNode;
            zeroNode.next = null;
        }
        return minList;
    }

    public static void main(String[] args){
        ListNode l1 = new ListNode(9);
        ListNode l2 = new ListNode(8);


        l1.next = l2;
        l2.next = null;

        ListNode l4 = new ListNode(1);

        l4.next = null;

        ListNode resultNode = addTwoNumbers(l1,l4);
        reverseList(resultNode);
    }


}
